<template>
  <a-button :loading="uploading" @click="exportExcel" type="primary" ghost>导出数据</a-button>
</template>
<script>
import XLSX from 'xlsx'
import { rateTypes, manageState, interestPay, interestCalc } from '@/dict'
import { getList } from '@/api/common'
import moment from 'moment'

export default {
  name: 'studentExport',
  props: ['dateRange', 'form', 'columns', 'keyWord'],
  data: () => ({
    uploading: false
  }),
  methods: {
    async exportExcel() {
      this.uploading = true
      /* 从表生成工作簿对象 */
      const form = Object.assign({}, this.form, { page: 1, limit: 99999 })
      for (let k in form) {
        if (!form[k]) {
          delete form[k]
        }
      }
      if (form.currency == '全部') {
        delete form.currency
      }
      if (this.keyWord) {
        form.keyWord = this.keyWord
      }
      if (!form.duration) {
        delete form.durationUnit
      }
      const res = await getList(form)

      const _list = res.result?.data?.list || []
      const list = _list.filter(el => el.agreementDate != 0 && el.contractDate != 0)
      const exportData = list.map(el => {
        const loanData = el.loanData
        console.log(el)
        if (loanData) {
          el.totalLoan = el.loanData.totalLoan
          el.principal = el.loanData.principal
          el.currency = el.loanData.currency
          const interestPayType = el.loanData.interestPayType
          el.interestPayType = interestPay.find(el => el.value == interestPayType).text
          const interestCalcType = el.loanData.interestCalcType
          el.interestCalcType = interestCalc.find(el => el.value == interestCalcType).text
          el.rate = el.loanData.rate
          el.agreementDate = el.agreementDate ? moment(el.agreementDate * 1000).format('YYYY-MM-DD') : '-'
          el.firstLoanTime = el.firstLoanTime ? moment(el.firstLoanTime * 1000).format('YYYY-MM-DD') : '-'
          el.sysEndTime = el.realEndTime != 0 ? el.realEndTime : el.sysEndTime
          el.sysEndTime = el.sysEndTime ? moment(el.sysEndTime * 1000).format('YYYY-MM-DD') : '-'
          el.projectStage = manageState.find(e => e.value == el.projectStage)?.text
          const durationUnit = el.loanData.durationUnit
          el.duration = el.loanData.duration + rateTypes.find(el => el.value == durationUnit).text
          const fee = el.loanData.fee
          fee &&
            fee.forEach(e => {
              if (e.feeType == 1) {
                el.limitFeeRate = e.rate
              } else if (e.feeType == 2) {
                el.commissionRate = e.rate
              } else if (e.feeType == 3) {
                el.otherFee = e.rate
              } else if (e.feeType == 4) {
                el.administrativeFeeRate = e.rate
              }
            })
        }
        const res = {}
        this.columns.forEach(col => {
          if (el[col.dataIndex]) {
            res[col.title] = el[col.dataIndex]
          }
        })
        return res
      })
      // console.log(exportData)

      var ws = XLSX.utils.json_to_sheet(exportData)
      /* 获取二进制字符串作为输出 */
      var wb = XLSX.utils.book_new() /*新建book*/
      XLSX.utils.book_append_sheet(wb, ws, '项目列表') /* 生成xlsx文件(book,sheet数据,sheet命名) */
      XLSX.writeFile(wb, '项目列表.xlsx') /*写文件(book,xlsx文件名称)*/
      this.uploading = false
    }
  }
}
</script>
